package cn.topcodes.tcsf.admin.web.integritycheck;

import cn.topcodes.tcsf.admin.service.IntegrityCheckCallback;
import cn.topcodes.tcsf.admin.service.IntegrityCheckRule;
import cn.topcodes.tcsf.admin.service.IntegrityCheckService;
import cn.topcodes.tcsf.admin.web.websocket.AdminWebsocketHelper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * Created by Unicorn on 2018/3/14.
 */
public class IntegrityCheckPushCallback implements IntegrityCheckCallback {

    @Autowired
    private IntegrityCheckService integrityCheckService;

    @PostConstruct
    public void init() {
        integrityCheckService.setCallback(this);
    }

    @Override
    public void onStart() {
        AdminWebsocketHelper.broadcastText("开始执行检查...");
    }

    @Override
    public void onRuleCheckBefore(IntegrityCheckRule rule) {
        AdminWebsocketHelper.broadcastText("执行检查规则 " + rule.getName() + " ...");
    }

    /**
     * 检查中
     *
     * @param rule
     * @param text
     */
    @Override
    public void onRuleChecking(IntegrityCheckRule rule, String text) {
        AdminWebsocketHelper.broadcastText("执行检查规则 " + rule.getName() + "：" + text);
    }

    /**
     * 检查发生错误
     *
     * @param rule
     * @param err
     */
    @Override
    public void onRuleCheckError(IntegrityCheckRule rule, String err) {
        AdminWebsocketHelper.broadcastText("执行检查规则 " + rule.getName() + "发生错误：" + err);
    }

    @Override
    public void onRuleChecked(IntegrityCheckRule rule, String result) {
        AdminWebsocketHelper.broadcastText("规则 " + rule.getName() + " 执行结果：" + result);
    }

    @Override
    public void onStop() {
        AdminWebsocketHelper.broadcastText("检查完成.");
    }
}
